import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
  path: '/login',
  name: 'login',
  component: () =>
      // 此处@代表的是绝对地址E:\顺义黑马就业87期学习\vue项目黑马头条\topline
      import('@/views/login')
},
{
  path: '/home',
  name: 'home',
  component: () =>
      import('@/views/home'),
  // 设置子级路由
  // 子级路由需要设置重定向
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    name: 'welcome',
    component: () =>
          import('@/views/welcome')
  },
  {
    path: '/article',
    name: 'article',
    component: () =>
          import('@/views/article')
  }
  ]

}
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({

  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  let token = window.sessionStorage.getItem('token')
  // 设置路由守卫的时候无论写不写内容必须要先写一个next()不然会全部拦截
  if (to.path !== '/login' && !token) {
    return next('/login')
  }
  next()
})

export default router
